﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class AddBranch : SOSecurityPage
    {
        private String action = "Create";
        //private String action = "Update";
        //private String action = "View";
        int successDel = 0;
        int successUpd = 0;
        int flag;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (!IsPostBack)
                {

                    txtEffectiveFrom.Text = System.DateTime.Today.ToShortDateString();
                    txtEffectiveTo.Text = System.DateTime.Today.ToShortDateString();

                }


                ValidateUserCredentials(Request.Url.LocalPath);

                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    action = Request.QueryString["a"].ToString();

                }


                if (action == "Create")
                {
                    if (!Page.IsPostBack)
                    {
                        this.Create();
                    }
                }
                else if (action == "u")
                {
                    if (!IsPostBack)
                    {
                        this.Update();
                    }



                }
                else if (action == "v")
                {

                    if (!Page.IsPostBack)
                    {
                        this.View();
                    }

                }
                //else if (action == "Delete")
                //{

                //    this.Delete();

                //                }

            }
            catch (Exception ex) { }

        }

        

        public void FillDropDown(string type)
        {
            StructureFacade fac = new StructureFacade();

            switch (type)
            {
                case "Group":
                    List<SalesOneGroup> grp = fac.GetGroups("GroupId ASC");
                    SalesOneGroup grsel = new SalesOneGroup();
                    grsel.GroupId = -1;
                    grsel.Name = "Select...";
                    grp.Insert(0, grsel);
                    GroupDropDownList.DataSource = grp;
                    GroupDropDownList.DataTextField = "Name";
                    GroupDropDownList.DataValueField = "GroupId";
                    GroupDropDownList.DataBind();
                    break;

                default:
                    break;
            }
        }

        public void Create()
        {
            StructureFacade branchFecade = new StructureFacade();
            CodeFacade coFac = new CodeFacade();

            lblHeader.Text = "Create Branch";
            btnDone.Text = "Create";
            //check this postback again
            Branch branchObject = new Branch();
            //brnchId_c
            //Page.Request.QueryString["brnchId_c"];

            if (Page.Request.Params["brnchId_c"] != null)
            {
            string branchstr = Page.Request.Params["brnchId_c"].ToString();
            Session["branchId"] = branchstr;

            int branchID = int.Parse(branchstr);
            branchObject = branchFecade.GetBranch(branchID);

            //Bid_u
            //string branchStr = Session["branchId"].ToString();

            GroupDropDownList.SelectedValue = branchObject.GroupId.ToString();
            }

            if (Page.Request.Params["grpId_c"] != null)
            {
                GroupDropDownList.SelectedValue = Page.Request.Params["grpId_c"].ToString();
            }



            List<Country> coun = coFac.GetCountries("CodeId ASC");
            Country cSel = new Country();

            cSel.CodeId = -1;
            cSel.Description = "Select...";
            coun.Insert(0, cSel);

            CountryDropDownList.DataSource = coun;
            CountryDropDownList.DataTextField = "Description";
            CountryDropDownList.DataValueField = "CodeId";
            CountryDropDownList.DataBind();


            if (!IsPostBack)
            {
                try
                {
                    FillDropDown("Group");
                }
                catch (Exception ex) { }
                }
        }

        #region delete not applied to branch module
        //public void Delete()
        //{
        //    string branchStr = Session["/Structure/ManageBranches.aspxview"].ToString();
        //    int branchID = int.Parse(branchStr);
        //    Branch branchObject = new Branch();
        //    StructureFacade strFacade = new StructureFacade();
        //    branchObject = strFacade.GetBranch(branchID);
        //    successDel = strFacade.DeleteBranch(branchObject);

        //    if (successDel == 1)
        //    {
        //        Response.Redirect("~/Structure/ManageBranches.aspx");
        //    }


        //} 
        #endregion

        public void Update()
        {
            CodeFacade coFac = new CodeFacade();

            StructureFacade branchFecade = new StructureFacade();
            Branch branchObject = new Branch();
            SalesOneGroup sog = new SalesOneGroup();
            lblHeader.Text = "Update Branch";
            btnDone.Text = "Update";
            FillDropDown("Group");

            string branchstr = Page.Request.Params["Bid_u"].ToString();
            Session["branchId"] = branchstr;
            //Bid_u
            //string branchStr = Session["branchId"].ToString();
            int branchID = int.Parse(branchstr);
            branchObject = branchFecade.GetBranch(branchID);
            int groupID = branchObject.GroupId;
            sog = branchFecade.GetGroup(groupID);
            


            //branchObject = branchFecade.GetBranch(branchID);

            List<Country> coun = coFac.GetCountries("CodeId ASC");
            Country cSel = new Country();

            cSel.CodeId = -1;
            cSel.Description = "Select...";
            coun.Insert(0, cSel);

            CountryDropDownList.DataSource = coun;
            CountryDropDownList.DataTextField = "Description";
            CountryDropDownList.DataValueField = "CodeId";
            CountryDropDownList.DataBind();

            GroupDropDownList.SelectedValue = branchObject.GroupId.ToString();
           // GroupDropDownList.SelectedValue = sog.;

            txtCode.Text = branchObject.BranchCode;
            txtName.Text = branchObject.BranchName;
            txtAdress1.Text = branchObject.Address1;
            txtAddress2.Text = branchObject.Address2;

            //CountryDropDownList.Items.FindByValue(branchObject.Country.ToString());

            //drpCountry.SelectedValue = drpCountry.Items.FindByText(usrObj.Country).Value;
            if (!string.IsNullOrEmpty(branchObject.Country))
            {
                CountryDropDownList.SelectedValue = CountryDropDownList.Items.FindByText(branchObject.Country).Value;
            }

            //CountryDropDownList.SelectedIndex = (branchObject.Country);
            txtCity.Text = branchObject.City;
            txtPostalCode.Text = branchObject.PostalCode;
            txtContactNo.Text = branchObject.ContactNo;
            txtEffectiveFrom.Text = branchObject.EffectiveFrom;
            txtEffectiveTo.Text = branchObject.EffectiveTo;

        }

        public void View()
        {
            Branch br = new Branch();
            SalesOneGroup gp = new SalesOneGroup();
            StructureFacade sf = new StructureFacade();
            CodeFacade cd = new CodeFacade();
            //CodeFacade coFac = new CodeFacade();

            lblHeader.Text = "View Branch";

            


            GroupLabel.Text = "Group :";
            CodeLabel.Text = "Code :";
            NameLabel.Text = "Name :";
            AddressLabel.Text = "Address :";
            CityLabel.Text = "City :";
            CountryLabel.Text = "Country :";
            PostalCodeLabel.Text = "Postal Code :";
            ContactNoLabel.Text = "Contact No :";
            EffectiveFromLabel.Text = "Effective From :";
            EffectiveToLabel.Text = "Effective To :";




            CountryDropDownList.Visible = false;
            GroupDropDownList.Visible = false;
            btnDone.Visible = false;
            txtCode.Visible = false;
            txtName.Visible = false;
            txtAdress1.Visible = false;
            txtAddress2.Visible = false;
            txtCity.Visible = false;
            txtPostalCode.Visible = false;
            txtContactNo.Visible = false;
            txtEffectiveFrom.Visible = false;
            txtEffectiveTo.Visible = false;
            Image1.Visible = false;
            Image2.Visible = false;

            //string branchstr = Session["branchId"].ToString();
            string branchstr = Page.Request.Params["Bid"].ToString();
            Session["branchId"] = branchstr;


            int branchID = int.Parse(branchstr);
            
            

            br = sf.GetBranch(branchID);
            gp = sf.GetGroup(br.GroupId);
            lblGroup1.Text = gp.Name;
            lblCode.Text = br.BranchCode;
            lblName.Text = br.BranchName;
            lblAddress1.Text = br.Address1;
            lblAddress2.Text = br.Address2;
           
            

            //Country ctr=cd.GetCountry(Convert.ToInt32(br.Country.ToString()));
            //lblCountry1.Text = ctr.Description;


            lblCountry1.Text = br.Country;
            //CountryDropDownList.Text = br.Country;
            //CountryDropDownList.Enabled = false;
            lblCity.Text = br.City;
            lblPostalCode.Text = br.PostalCode;
            lblContactNo.Text = br.ContactNo;
            lblEffectiveFrom.Text = br.EffectiveFrom;
            lblEffectiveTo.Text = br.EffectiveTo;

            

        }

        protected void btnCan_Click(object sender, EventArgs e)
        {
            Session.Remove("branchId");
            Response.Redirect("~/Structure/ManageBranches.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void btnDone_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["a"]))
            {

                SalesOneGroup group = new SalesOneGroup();

                StructureFacade sf = new StructureFacade();
                CodeFacade coFac = new CodeFacade();
                string branchStr = Session["branchId"].ToString();
                int branchID = int.Parse(branchStr);
                Branch br = new Branch();
                br = sf.GetBranch(branchID);
                group = sf.GetGroup(Convert.ToInt32(GroupDropDownList.SelectedValue.ToString()));

                br.GroupId = int.Parse(GroupDropDownList.SelectedValue);
                // br = sf.GetBranch(branchID);
                br.Address1 = txtAdress1.Text;
                br.Address2 = txtAddress2.Text;
                br.BranchCode = txtCode.Text;
                br.BranchName = txtName.Text;
                br.City = txtCity.Text;

                
                br.PostalCode = txtPostalCode.Text;
                

                br.ContactNo = txtContactNo.Text;
                br.Country = CountryDropDownList.SelectedValue;
                br.EffectiveFrom = txtEffectiveFrom.Text;
                br.EffectiveTo = txtEffectiveTo.Text;

                DateTime dFrom = Convert.ToDateTime(txtEffectiveFrom.Text);
                DateTime dTo = Convert.ToDateTime(txtEffectiveTo.Text);



                if (dFrom < Convert.ToDateTime(group.EffectFrom.ToString()) || dTo > Convert.ToDateTime(group.EffectTo.ToString()))
                {
                    MassageSummery("IIS519");
                    flag = 1;

                }


                if (flag != 1)
                {

                    successUpd = sf.UpdateBranch(br);

                    if (successUpd == 1)
                    {
                        Session["sccBranch"] = "Successfully Updated";
                        //Response.Redirect("~/Structure/ManageBranches.aspx");
                    }
                    else
                    {
                        //txtAddress2.Text = successUpd.ToString();
                        Session["failBranch"] = "Fail to Update";

                    }

                    Session.Remove("branchId");
                }
            }
            else
            {
                StructureFacade branchFecade = new StructureFacade();
                SalesOneGroup group = new SalesOneGroup();
                Branch branch = new Branch();
                DateTime dFrom = Convert.ToDateTime(txtEffectiveFrom.Text);
                DateTime dTo = Convert.ToDateTime(txtEffectiveTo.Text);
                group = branchFecade.GetGroup(Convert.ToInt32(GroupDropDownList.SelectedValue.ToString()));
                branch.BranchCode = txtCode.Text;
                branch.BranchName = txtName.Text;
                branch.Address1 = txtAdress1.Text;
                branch.Address2 = txtAddress2.Text;
                branch.GroupId = int.Parse(GroupDropDownList.SelectedValue);
                //branch.GroupName = GroupDropDownList.SelectedValue;                
                branch.Country = CountryDropDownList.Text;
                //branch.Country = CountryDropDownList.SelectedValue;
                branch.City = txtCity.Text;

                
                branch.PostalCode = txtPostalCode.Text;
               

                branch.ContactNo = txtContactNo.Text;
                branch.EffectiveFrom = txtEffectiveFrom.Text;

                if (Convert.ToDateTime(txtEffectiveFrom.Text) < DateTime.Today)
                {
                    //lblResult.Visible = true;
                    //lblResult.Text = "Effective from date must a future date";
                    //lblResult.ForeColor = System.Drawing.Color.Red;
                    MassageSummery("IIS507");

                    flag = 1;
                }

                if (dFrom < Convert.ToDateTime(group.EffectFrom.ToString()) || dTo > Convert.ToDateTime(group.EffectTo.ToString()))
                {
                    MassageSummery("IIS519");
                    flag = 1;

                }

                branch.EffectiveTo = txtEffectiveTo.Text;


                try
                {
                    if (flag != 1)
                    {
                        branchFecade.InsertBranch(branch);
                        Session["sccBranch"] = "Successfully Inserted";
                    }
                }
                catch (Exception)
                {
                    Session["failBranch"] = "Fail to insert";
                }

            }
            if (flag != 1)
            {
                Response.Redirect("~/Structure/ManageBranches.aspx");
            }
        }

       

        

    }
}
